<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户ID " prop="userId">
        <el-input v-model="queryParams.userId" placeholder="请输入用户ID " clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="姓名 " prop="userName">
        <el-input v-model="queryParams.userName" placeholder="请输入姓名 " clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input v-model="queryParams.mobile" placeholder="请输入手机号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="queryParams.email" placeholder="请输入邮箱" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="抬头类型" prop="invoiceHeaderType">
        <el-select v-model="queryParams.invoiceHeaderType" placeholder="抬头类型" @keyup.enter.native="handleQuery"
          clearable>
          <el-option v-for="(item, index) in traslateTypeOptions" :key="index" :label="item.name"
            :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="发票类别" prop="type">
        <el-select v-model="queryParams.type" placeholder="发票类别" @keyup.enter.native="handleQuery" clearable>
          <el-option v-for="(item, index) in rechargeTypeOptions" :key="index" :label="item.name"
            :value="item.id"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="审核状态" prop="auditStatus">
        <el-select v-model="queryParams.auditStatus" placeholder="审核状态" @keyup.enter.native="handleQuery" clearable>
          <el-option v-for="(item, index) in traslateStatusOptions" :key="index" :label="item.name"
            :value="item.id"></el-option>
        </el-select>
      </el-form-item>


      <el-form-item label="申请日期">
        <el-date-picker v-model="dateRange" type="datetimerange" align="right" start-placeholder="开始日期"
          end-placeholder="结束日期" :default-time="['00:00:00', '23:59:59']" value-format="timestamp"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">

      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['translate:translate:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="translateList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="用户ID" align="center" prop="userId" />
      <el-table-column label="姓名 " align="center" prop="userName" />
      <el-table-column label="手机号" align="center" prop="mobile" />
      <el-table-column label="抬头类型" align="center" prop="invoiceHeaderType">
        <template slot-scope="scope">
          <span>{{ traslateType[scope.row.invoiceHeaderType] }}</span>
        </template>
      </el-table-column>
      <el-table-column label="税号" align="center" prop="dutyParagraph" />
      <el-table-column label="发票抬头" align="center" prop="invoiceHeader" />
      <el-table-column label="服务类型" align="center" prop="invoiceContent">

        <template slot-scope="scope">
          <span>{{ traslateContent[scope.row.invoiceContent] }}</span>
        </template>
      </el-table-column>
      <el-table-column label="邮箱" align="center" prop="email" />
      <el-table-column label="开票金额" align="center" prop="money" />
      <el-table-column label="发票类别" align="center" prop="type">

        <template slot-scope="scope">
          <span>{{ rechargeType[scope.row.type] }}</span>
        </template>
      </el-table-column>
      <el-table-column label="绑定单号" align="center" prop="orderSn" />
      <el-table-column label="审核状态 " align="center" prop="auditStatus">

        <template slot-scope="scope">
          <span>{{ traslateStatus[scope.row.auditStatus] }}</span>
        </template>
      </el-table-column>
      <el-table-column label="申请时间" align="center" prop="applyTime">

        <template slot-scope="scope">
          <span>{{ scope.row.applyTime | timestampToDate }}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">

        <template slot-scope="scope">
          <el-button v-if="0 == scope.row.auditStatus" size="mini" type="text" icon="el-icon-edit"
            @click="success(scope.row, scope.$index)" v-hasPermi="['translate:translate:add']">通过</el-button>

          <el-button v-if="0 == scope.row.auditStatus" size="mini" type="text" icon="el-icon-delete"
            @click="auditCancel(scope.row, scope.$index)" v-hasPermi="['translate:translate:edit']">不通过</el-button>

          <el-button v-if="2 == scope.row.auditStatus && !scope.row.file" size="mini" type="text" icon="el-icon-edit"
            @click="uploadTranslate(scope.row, scope.$index)"
            v-hasPermi="['translate:translate:upload']">上传发票</el-button>

          <el-button v-if="2 == scope.row.auditStatus && scope.row.file" size="mini" type="text" icon="el-icon-edit"
            @click="lookTranslate(scope.row, scope.$index)" v-hasPermi="['translate:translate:look']">查看发票</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
    <upload :visible="visible" :id="selectId" @close=closeUpload>
    </upload>
    <look :visible="translateVisible" :pic="file" @close="closeTranslate"></look>

    <el-dialog title="审核原因"  :visible.sync="auditReasonShow" @close="auditClose">
      <el-form ref="auditForm" :model="auditSubmit" label-position="left" label-width="80px" :rules="rules">
        <el-form-item label="审核原因" prop="auditReason">
          <el-input placeholder="请输入审核原因" v-model="auditSubmit.auditReason"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="auditReasonShow = false">取 消</el-button>
        <el-button type="primary" @click="cancel">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { applyTranslate, listTranslate, updateTranslate } from "@/api/translate/translate";
import { RECHARGE_TRANSLATE, TRANSLATE_CONTENT, TRASLATE_APPLY, TRASLATE_TYPE, TRASLATE_APPLY_OPTIONS, TRASLATE_TYPT_OPTIONS, RECHARGE_TRANSLATE_OPTIONS } from '@/constant/const';
import { timestampToDate } from '@/utils/time';
export default {
  name: "Translate",
  data() {
    return {
      // 遮罩层
      loading: true,
      visible: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      file: undefined,
      // 选择的 id
      selectId: 0,
      translateVisible: false,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 发票表格数据
      translateList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        userName: null,
        mobile: null,
        assocId: null,
        orderSn: null,
        type: null,
        email: null,
        dutyParagraph: null,
        invoiceHeader: null,
        invoiceContent: null,
        invoiceHeaderType: null,
        money: null,
        applyTime: null,
        auditStatus: null,
        file: null,
        params: {
          startTime: null,
          endTime: null
        }
      },
      rechargeType: RECHARGE_TRANSLATE,
      rechargeTypeOptions: RECHARGE_TRANSLATE_OPTIONS,
      traslateStatus: TRASLATE_APPLY,
      traslateStatusOptions: TRASLATE_APPLY_OPTIONS,
      traslateType: TRASLATE_TYPE,
      traslateTypeOptions: TRASLATE_TYPT_OPTIONS,
      traslateContent: TRANSLATE_CONTENT,
      isSearch: false,
      dateRange: [],
      auditReasonShow: false,
      auditObj: null,
      auditIndex: null,
      auditSubmit: {
        auditReason: '',
      },
      rules: {
        auditReason: [
          { required: true, message: "请输入审核原因", trigger: "blur" }
        ]
      }
    };
  },

  components: {
    upload: () => import('./component/uploadTranslate.vue'),
    look: () => import('./component/lookTranslate.vue'),
  },

  filters: {
    timestampToDate,
  },

  created() {
    this.getList();
  },
  methods: {
    auditClose(){
      this.$refs.auditForm.resetFields();
    },

    /**
     * 关闭上传窗口
     */
    closeUpload(formData, visible) {

      this.visible = visible;

      if (!formData) {
        return
      }

      this.translateList[this.selectIndex].file = formData.file;

    },

    closeTranslate(formData, visible) {
      this.translateVisible = visible
    },

    success(row, index) {
      applyTranslate({
        id: row.id,
        auditStatus: 2,
        orderSn: row.orderSn
      }, this).then(res => {
        console.log("res: ", res);
        this.translateList[index].auditStatus = 2;
      });
    },
    auditCancel(row, index) {
      this.auditObj = row;
      this.auditIndex = index;
      this.auditReasonShow = true;
    },
    cancel() {
      this.$refs.auditForm.validate((valid) => {
        if (valid) {
          applyTranslate({
            id: this.auditObj.id,
            auditStatus: 1,
            reason: this.auditSubmit.auditReason,
            orderSn: this.auditObj.orderSn
          }, this).then(res => {
            this.translateList[this.auditIndex].auditStatus = 1;
            this.auditReasonShow = false;
          });
        }
      })

    },

    lookTranslate(row) {
      console.log("row", row);
      this.translateVisible = true;
      this.selectId = row.id;
      this.file = row.file;
    },

    uploadTranslate(row, index) {

      this.selectId = row.id;

      this.selectIndex = index;
      this.visible = true;

    },

    /** 查询发票列表 */
    getList() {
      let params = {};
      if (this.isSearch) {
        params = Object.assign({}, this.queryParams);
        params['params']['startTime'] = (this.dateRange && this.dateRange.length) ? this.dateRange[0] / 1000 : '';
        params['params']['endTime'] = (this.dateRange && this.dateRange.length) ? this.dateRange[1] / 1000 : '';
      } else {
        params = {
          pageNum: this.queryParams.pageNum,
          pageSize: this.queryParams.pageSize
        }
      }
      this.loading = true;
      listTranslate(params).then(response => {
        this.translateList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        userName: null,
        mobile: null,
        assocId: null,
        orderSn: null,
        type: null,
        email: null,
        dutyParagraph: null,
        invoiceHeader: null,
        invoiceContent: null,
        invoiceHeaderType: null,
        money: null,
        applyTime: null,
        auditStatus: null,
        file: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.isSearch = true;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.isSearch = false;
      this.queryParams.pageNum = 1;
      this.dateRange = []
      this.getList();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },

    /** 导出按钮操作 */
    handleExport() {
      this.queryParams.ids = this.ids;
      this.download('translate/translate/export', {
        ...this.queryParams,
        ids: this.ids.length ? this.ids : undefined
      }, `translate_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
